/*
 * @Author: monster.huangwei
 * @Date: 2018-11-04 14:05:55
 * @Last Modified by: monster.huangwei
 * @Last Modified time: 2019-02-13 11:41:43
 */
(function () {
  Date.prototype.format = function (fmt) {
    var o = {
      'M+': this.getMonth() + 1, // 月份
      'd+': this.getDate(), // 日
      'h+': this.getHours(), // 小时
      'm+': this.getMinutes(), // 分
      's+': this.getSeconds(), // 秒
      'q+': Math.floor((this.getMonth() + 3) / 3), // 季度
      'S': this.getMilliseconds() // 毫秒
    }

    if (/(y+)/.test(fmt)) {
      fmt = fmt.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length))
    }

    for (var k in o) {
      if (new RegExp('(' + k + ')').test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
      }
    }
    return fmt
  }

  Number.prototype.add = function (arg) { // 加法精确求值
    var s1 = this.toString()

    var s2 = arg.toString()
    var r1 = 0

    var r2 = 0

    try {
      if (s1.indexOf('.') >= 0) {
        r1 = s1.split('.')[1].length
      }

      if (s2.indexOf('.') >= 0) {
        r2 = s2.split('.')[1].length
      }
    } catch (e) {

    }

    var m = Math.pow(10, Math.max(r1, r2))

    return (this * m + arg * m) / m
  }

  Number.prototype.div = function (arg) { // 除法精确求值
    var s1 = this.toString()

    var s2 = arg.toString()
    var t1 = 0

    var t2 = 0

    try {
      if (s1.indexOf('.') >= 0) {
        t1 = s1.split('.')[1].length
      }

      if (s2.indexOf('.') >= 0) {
        t2 = s2.split('.')[1].length
      }
    } catch (e) {

    }

    var r1 = Number(this.toString().replace('.', ''))
    var r2 = Number(arg.toString().replace('.', ''))

    return (r1 / r2) * Math.pow(10, t2 - t1)
  }

  // eslint-disable-next-line no-extend-native
  Number.prototype.formatMoney = function () { // 将数值转化成0.00格式
    var s_x = ''

    try {
      if (isNaN(this)) {
        return s_x
      }

      var f_x = Number(parseInt(this.mul(100))).div(100)
      s_x = f_x.toString()

      var pos_decimal = s_x.indexOf('.')

      if (pos_decimal < 0) {
        pos_decimal = s_x.length
        s_x += '.'
      }

      while (s_x.length <= pos_decimal + 2) {
        s_x += '0'
      }

      return s_x
    } catch (e) {

    }

    return s_x
  }

  Number.prototype.mul = function (arg) { // 乘法精确求值
    var s1 = this.toString()

    var s2 = arg.toString()
    var m = 0

    try {
      if (s1.indexOf('.') >= 0) {
        m += s1.split('.')[1].length
      }

      if (s2.indexOf('.') >= 0) {
        m += s2.split('.')[1].length
      }
    } catch (e) {

    }

    var r1 = Number(s1.replace('.', ''))
    var r2 = Number(s2.replace('.', ''))

    return r1 * r2 / Math.pow(10, m)
  }

  Number.prototype.sub = function (arg) { // 减法精确求值
    return this.add(-arg)
  }

  Number.prototype.toPercent = function () { // 将数值转化为百分比
    var aa = this.mul(100)
    return '' + aa + '%'
  }

  /**
   * 判断字符串是否以str结尾
   * @param str
   * @returns {boolean}
   */
  String.prototype.endWith = function (str) {
    if (Tools.check.isEmpty(str)) return false
    if (this.substring(this.length - str.length) == str) return true
    return false
  }

  String.prototype.hideAddress = function () {
    var wArray = ['街', '路']
    var retStr

    var pos = 0

    if (Tools.check.isEmpty(this)) {
      return ''
    }

    for (var i = 0; i < wArray.length; i++) {
      pos = this.indexOf(wArray[i])

      if (pos > -1) {
        retStr = '*' + wArray[i] + this.substr(pos + 1)
        return retStr
      }
    }

    if (this.length > 10) {
      retStr = '*' + this.substr(-10)
    } else {
      retStr = this.toString()
    }

    return retStr
  }

  String.prototype.hideName = function () {
    var retStr = this.toString().substr(0)

    if (Tools.check.isEmpty(this)) {
      return ''
    }

    for (var i = 1; i < this.length; i++) {
      retStr += '*'
    }

    return retStr
  }

  String.prototype.hideCenter = function () {
    if (Tools.check.isEmpty(this)) {
      return ''
    }

    var len = this.length - 1
    var str = ''

    while (len > 0) {
      str = str + 'X'
      len--
    }

    return str + this.substr(-1)
  }

  /**
   * 判断字符串是否以str开头
   * @param str
   * @returns {boolean}
   */
  String.prototype.startWith = function (str) {
    if (Tools.check.isEmpty(str)) return false
    if (this.substring(0, str.length) == str) return true
    return false
  }
})()
var Base64 = function () {
  var _keyStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='

  this.encode = function (input) {
    var output = ''

    var chr1, chr2, chr3
    var enc1, enc2, enc3, enc4

    var i = 0

    input = _utf8_encode(input)

    while (i < input.length) {
      chr1 = input.charCodeAt(i++)
      chr2 = input.charCodeAt(i++)
      chr3 = input.charCodeAt(i++)

      enc1 = chr1 >> 2
      enc2 = ((chr1 & 3) << 4) | (chr2 >> 4)
      enc3 = ((chr2 & 15) << 2) | (chr3 >> 6)
      enc4 = chr3 & 63

      if (isNaN(chr2)) {
        enc3 = enc4 = 64
      } else if (isNaN(chr3)) {
        enc4 = 64
      }

      output = output + _keyStr.charAt(enc1) +
        _keyStr.charAt(enc2) +
        _keyStr.charAt(enc3) +
        _keyStr.charAt(enc4)
    }

    return output
  }

  this.decode = function (input) {
    var output = ''

    var chr1, chr2, chr3
    var enc1, enc2, enc3, enc4

    var i = 0

    input = input.replace(/[^A-Za-z0-9\+\/\=]/g, '')

    while (i < input.length) {
      enc1 = _keyStr.indexOf(input.charAt(i++))
      enc2 = _keyStr.indexOf(input.charAt(i++))
      enc3 = _keyStr.indexOf(input.charAt(i++))
      enc4 = _keyStr.indexOf(input.charAt(i++))

      chr1 = (enc1 << 2) | (enc2 >> 4)
      chr2 = ((enc2 & 15) << 4) | (enc3 >> 2)
      chr3 = ((enc3 & 3) << 6) | enc4

      output = output + String.fromCharCode(chr1)

      if (enc3 != 64) {
        output = output + String.fromCharCode(chr2)
      }
      if (enc4 != 64) {
        output = output + String.fromCharCode(chr3)
      }
    }

    output = _utf8_decode(output)

    return output
  }

  var _utf8_encode = function (input) {
    var output = ''

    input = input.replace(/\r\n/g, '\n')

    for (var i = 0; i < input.length; i++) {
      var chr = input.charCodeAt(i)

      if (chr < 128) {
        output += String.fromCharCode(chr)
      } else if ((chr > 127) && (chr < 2048)) {
        output += String.fromCharCode((chr >> 6) | 192)
        output += String.fromCharCode((chr & 63) | 128)
      } else {
        output += String.fromCharCode((chr >> 12) | 224)
        output += String.fromCharCode(((chr >> 6) & 63) | 128)
        output += String.fromCharCode((chr & 63) | 128)
      }
    }

    return output
  }

  var _utf8_decode = function (input) {
    var output = ''

    var i = 0
    var chr1 = 0

    var chr2 = 0

    var chr3 = 0

    while (i < input.length) {
      chr1 = input.charCodeAt(i)

      if (chr1 < 128) {
        output += String.fromCharCode(chr1)
        i++
      } else if ((chr1 > 191) && (chr1 < 224)) {
        chr2 = input.charCodeAt(i + 1)
        output += String.fromCharCode(((chr1 & 31) << 6) | (chr2 & 63))
        i += 2
      } else {
        chr2 = input.charCodeAt(i + 1)
        chr3 = input.charCodeAt(i + 2)
        output += String.fromCharCode(((chr1 & 15) << 12) | ((chr2 & 63) << 6) | (chr3 & 63))
        i += 3
      }
    }

    return output
  }
}

var base64 = new Base64()

var Tools = {
  check: {
    // 校验对象是否为空
    isEmpty: function (obj) {
      if ((Object.prototype.toString.call(obj) === '[object Array]' && obj.length == 0) ||
        (Object.prototype.toString.call(obj) === '[object Object]' && JSON.stringify(obj).length <= 2) ||
        obj == null || obj == undefined || ('' + obj) == '') {
        return true
      }
      return false
    },
    // 校验对象是否为非空
    isNoEmpty: function (obj) {
      if ((Object.prototype.toString.call(obj) === '[object Array]' && obj.length == 0) ||
        (Object.prototype.toString.call(obj) === '[object Object]' && JSON.stringify(obj).length <= 2) ||
        obj == null || obj == undefined || ('' + obj) == '') {
        return false
      }
      return true
    },
    // 校验字符串是否为中文
    isChinese: function (str) {
      var temp = Tools.convert.trim(str);
      if (Tools.check.isEmpty(temp)) return false;
      for (var i = 0; i < temp.length; i++) {
          var tempChar = temp[i];
          if (escape(tempChar).indexOf("%u") < 0) return false;
      }
      return true;
    },
    // 校验数字是否为整数
    isInteger: function (str) {
      var reg = /^[0-9]*[1-9][0-9]*$/;
      return reg.test(str);
    },
    // 校验字符串的最大长度
    maxLength: function (val, comparedLength) {
      if (('' + val).length > comparedLength) {
          return false;
      }
      return true;
    },
    // 校验字符串的最小长度
    minLength: function (val, comparedLength) {
      if (('' + val).length < comparedLength) {
          return false;
      }
      return true;
    },
    // 校验字符串的长度
    isEqualLength: function (val, comparedLength) {
      if (('' + val).length != comparedLength) {
          return false;
      }
      return true;
    },
    // 校验颜色的hash值
    isColor16Hex: function(str) {
      if (Object.prototype.toString.call(str) !== '[object String]') return false;
      let reg = /^#[\da-f]{3}([\da-f]{3})?$/i
      return reg.test(str)
    },
    // 校验json对象
    isJson: obj => {
      if (Tools.check.isEmpty(obj)) return false;
      if (Object.prototype.toString.call(obj) == '[object Object]') return true;
      return false
    },
    // 校验邮箱格式
    isEmail: str => {
      var reg = /^(\w)+(\.\w+)*@(\w)+((\.\w{2,3}){1,3})$/;
      return reg.test(str)
    }
  },
  convert: {
    /**
     * 将input转换为decimal
     * @param input 输入值
     * @param intLength整数部分的最大长度
     * @param decLength 小数部分的最大长度
     * @returns {*}
     */
    inputDecimal: function (input, intLength, decLength) {
      if (Tools.check.isEmpty(input)) return input;
        if (input.indexOf(".") == -1) {
          return Tools.convert.inputIntegerWithLength(input, intLength);
        } else {
          var arr = input.split(".");
          // 整数部分
          var intPart = Tools.convert.inputIntegerWithLength(arr[0], intLength);
          // 小数部分
          var decPart = Tools.convert.inputIntegerWithLength(arr[1], decLength);
          return intPart + "." + decPart;
        }
    },
    /**
     * 将input转换为Integer
     * @param input 输入值
     * @returns {*}
     */
    inputInteger: function (input) {
      if (Tools.check.isEmpty(input)) return input;
      return input.replace(/\D/, "");
    },
    /**
     * 将input转换长度为intLength的Integer
     * @param input 输入值
     * @param intLength 整数的最大长度
     * @returns {*}
     */
    inputIntegerWithLength: function (input, intLength) {
      if (Tools.check.isEmpty(input)) return input;
      var retVal = Tools.convert.inputInteger(input);
      if (Tools.check.isInteger(intLength) && retVal.length > intLength) {
        retVal = retVal.substring(0, intLength);
      }
      return retVal;
    },
    /**
     * 限制input的输入长度
     * @param input input 输入值
     * @param maxLength 字符串的最大长度
     * @returns {*}
     */
    inputStringWithLength: function (input, maxLength) {
      if (Tools.check.isEmpty(input)) return input;
      if (Tools.check.isInteger(maxLength) && input.length > maxLength) {
        return input.substring(0, maxLength);
      }
        return input;
    },
    // 去除空格
    trim: function (str) {
      if (Tools.check.isEmpty(str)) {
        return str;
      } else {
        return ("" + str).replace(/ /g, "");
      }
    },
    // 去除头尾空格
    sidesTrim: str => {
      if (Tools.check.isEmpty(str)) {
        return str
      } else {
        return str.replace(/^\s*|\s*$/g,"")
      }
    },
    // 对象转base64
    jsonToBase64: obj => {
      if (Tools.check.isEmpty(obj)) return '';
      return base64.encode(JSON.stringify(obj))
    },
    // base64转对象
    base64ToJson: str => {
      if (Tools.check.isEmpty(str)) return {};
      return JSON.parse(base64.decode(str))
    },
    // 将字符串或数字转换为0,000.00
    convertMoney: function (str, val) {
      val = Tools.convert.trim(val);
      return str + Number(val).div(100).formatMoney().replace(/\d{1,3}(?=(\d{3})+(\.\d*)?$)/g, '$&,')
    },
    // 隐藏银行卡卡号信息
    hideCardNumberInfo: function (cardNumber) {
      var ret = Tools.convert.trim(cardNumber);
      try {
          if (Tools.check.isEmpty(ret)) return ret;
          ret = String(ret);

          if (ret.length <= 10) return ret;
          var prefix = ret.substring(0, 6);
          var suffix = ret.substring(ret.length - 4);
          var len = ret.length - 10;

          ret = prefix;
          while ((len--) > 0) {
              ret = ret + "*";
          }
          ret = ret + suffix;
      } catch (e) {
          console.error(e.message);
      }
      ret = Tools.format.stringWithSpace(ret, 4);
      return ret;
    },
    // 分割字符串
    stringWithSpace: function (value, firstLen, splitLen) {
      if (Tools.check.isEmpty(value)) return value;
      value = Tools.convert.trim(value);
      splitLen = Tools.check.isEmpty(splitLen) ? 0 : parseInt(splitLen);
      splitLen = splitLen < 0 ? 0 : splitLen;
      firstLen = Tools.check.isEmpty(firstLen) ? 0 : parseInt(firstLen);
      firstLen = firstLen < 0 ? 0 : firstLen;
      var retValue = "";
      for (var i = 0; i < value.length; i++) {
        retValue += value.split('')[i];
        if (firstLen > 0 && i + 1 < firstLen) {
          continue;
        } else if (firstLen > 0 && i + 1 == firstLen) {
          retValue += ' ';
        } else if ((i - firstLen + 1) % splitLen == 0) {
          retValue += ' ';
        }
      }
      return retValue.trim();
    },
    // 数组去重
    arrayUniq: (obj) => {
      if (Tools.check.isEmpty(obj)) return '';
      var arr = obj.filter((item, index, shef) => {
        return shef.indexOf(item) === index
      })
      return arr
    },
    // 获取浏览器URL中查询字符串中的参数
    getUrlParmas: () => {
      var sHref = window.location.href;
      var args = sHref.split('?');
      if(args[0] == sHref){
          return "";
      }
      var arr = args[1].split('&');
      var obj = {};
      for(var i = 0;i< arr.length;i++){
          var arg = arr[i].split('=');
          obj[arg[0]] = arg[1];
      }
      return obj;
    }
  }
}
//判断访问终端
var browser = {
  versions:function(){
      var u = navigator.userAgent, app = navigator.appVersion;
      return {
          trident: u.indexOf('Trident') > -1, //IE内核
          presto: u.indexOf('Presto') > -1, //opera内核
          webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核
          gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1,//火狐内核
          mobile: !!u.match(/AppleWebKit.*Mobile.*/), //是否为移动终端
          ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
          android: u.indexOf('Android') > -1 || u.indexOf('Adr') > -1, //android终端
          iPhone: u.indexOf('iPhone') > -1 , //是否为iPhone或者QQHD浏览器
          iPad: u.indexOf('iPad') > -1, //是否iPad
          webApp: u.indexOf('Safari') == -1, //是否web应该程序，没有头部与底部
          weixin: u.indexOf('MicroMessenger') > -1, //是否微信 （2015-01-22新增）
          qq: u.match(/\sQQ/i) == " qq" //是否QQ
      };
  }(),
  language:(navigator.browserLanguage || navigator.language).toLowerCase()
}
export { Tools, browser }
